package com.cloudptt.api.pjsip.api;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.res.Resource;

public class BaseCallConfig implements OnSharedPreferenceChangeListener
{
	private static String THIS_FILE = "BaseCallConfig";
	private static final String Call_Gsm_First = "gsm_first";
	private static final String Call_PCall_Speak_On = "pcall_speak_on";
	private static final String Timer_Nego ="timer_nego";
	private static final String Timer_KeepAlive_Wifi ="timer_keepAlive_wifi";
	private static final String Timer_KeepAlive_Mobile ="timer_keepAlive_mobile";
	public static final String VIDEO_ENABLE = "video_enable";
	
	public static final String Timer_Option ="timer_option";
	public final static int Default_Timer_Option = 20;
	public static final String Timer_BLN ="timer_bln";
	public final static int Default_Timer_BLN = 5;
	
	protected static final String GroupCall_Type ="group_call_type";
	public final static int GroupCall_Type_Muti = 0;
	public final static int GroupCall_Type_Single = 1;
	protected int groupCallType = GroupCall_Type_Muti;
	
	/* add by xu for 支持IMSI登录 @20170804 */
	protected static final String Login_Type ="login_type";
	public final static int Login_Type_IMEI = 0;
	public final static int Login_Type_IMSI = 1;	
	protected int loginType = Login_Type_IMEI;
	


	protected boolean isGsmFirst = false;
	protected boolean privateSpeakerOn = true;
//	protected int negoTimer = NegotiateTimer.Default_Timer_Nego;
//
//	protected int keepAliveTimer_Wifi = NegotiateTimer.Default_Timer_KeepAlive_Wifi;
//	protected int keepAliveTimer_Mobile = NegotiateTimer.Default_Timer_KeepAlive_Mobile;
	
	protected int timerOption = Default_Timer_Option;

	protected boolean videoEnable = false;
	protected SharedPreferences sp = null;
	private Context ctx = null;
	
	public BaseCallConfig()
	{}
	public BaseCallConfig(Context ctx)
	{
		this.ctx = ctx;
		this.sp = PreferenceManager.getDefaultSharedPreferences(ctx);
		this.sp.registerOnSharedPreferenceChangeListener(this);
		
		this.isGsmFirst = sp.getBoolean(Call_Gsm_First, false);
		this.privateSpeakerOn = sp.getBoolean(Call_PCall_Speak_On, true);
		Log.d(THIS_FILE,"Call_Gsm_First = " + this.isGsmFirst + " Call_PCall_Speak_On = " + this.privateSpeakerOn);
		
//		String value = sp.getString(Timer_Nego, "" + NegotiateTimer.Default_Timer_Nego);
//		this.negoTimer = CheckData.parseInt(value);
//		value = sp.getString(Timer_KeepAlive_Wifi, "" + NegotiateTimer.Default_Timer_KeepAlive_Wifi);
//		this.keepAliveTimer_Wifi = CheckData.parseInt(value);
//		value = sp.getString(Timer_KeepAlive_Mobile, "" + NegotiateTimer.Default_Timer_KeepAlive_Mobile);
//		this.keepAliveTimer_Mobile = CheckData.parseInt(value);
		this.videoEnable = sp.getBoolean( VIDEO_ENABLE, false );
		
		String value = sp.getString(Timer_Option, "" + Default_Timer_Option);
		this.timerOption = CheckData.parseInt(value);
		
		groupCallType = sp.getInt(GroupCall_Type, GroupCall_Type_Muti);
		
		loginType = sp.getInt(Login_Type, Login_Type_IMEI);
		
//		StringBuffer strBuf = new StringBuffer();
//		strBuf.append("negoTimer = ").append(negoTimer).append("  ");
//		strBuf.append("keepAliveTimer_Wifi = ").append(keepAliveTimer_Wifi).append("  ");
//		strBuf.append("keepAliveTimer_Mobile = ").append(negoTimer).append("  ");
//		strBuf.append("videoEnable = ").append(videoEnable).append("  ");
//		strBuf.append("timer_Option = ").append(timerOption).append("  ");
//		strBuf.append("groupCallType = ").append(groupCallType).append("  ");
		
		Log.e(THIS_FILE, "init " +toString());
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) 
	{
		// TODO Auto-generated method stub
		if(null == key)
		{
			Log.e(THIS_FILE,"onSharedPreferenceChanged key = null");
			return;
		}
		if(key.equals(Call_Gsm_First))
		{
			this.isGsmFirst = sharedPreferences.getBoolean(key, true);
			Log.e(THIS_FILE,"onSharedPreferenceChanged Call_Gsm_First = " + this.isGsmFirst );
		}
		else if(key.equals(Call_PCall_Speak_On))
		{
			this.privateSpeakerOn = sharedPreferences.getBoolean(key, true);
			Log.e(THIS_FILE,"onSharedPreferenceChanged Call_PCall_Speak_On = " + this.privateSpeakerOn);
		}
//		else if(key.equals(Timer_Nego))
//		{
//			String value = sharedPreferences.getString(key, "" + NegotiateTimer.Default_Timer_Nego);
//			this.negoTimer = CheckData.parseInt(value);
//		}
//		else if(key.equals(Timer_KeepAlive_Wifi))
//		{
//			String value = sharedPreferences.getString(key, "" + NegotiateTimer.Default_Timer_KeepAlive_Wifi);
//			this.keepAliveTimer_Wifi = CheckData.parseInt(value);
//		}
//		else if(key.equals(Timer_KeepAlive_Mobile))
//		{
//			String value = sharedPreferences.getString(key, "" + NegotiateTimer.Default_Timer_KeepAlive_Mobile);
//			this.keepAliveTimer_Mobile = CheckData.parseInt(value);
//		}
		else if( key.equals( VIDEO_ENABLE ) ){
			this.videoEnable = sharedPreferences.getBoolean( key, false );
		}
		else if(key.equals(Timer_Option))
		{
			String value = sp.getString(Timer_Option, "" + Default_Timer_Option);
			int val = CheckData.parseInt(value);
			if(this.timerOption != val)
			{
				this.timerOption = val;
				String str = Resource.getString("effect_reboot");
				CommDlg.showShortToast(ctx, str);
			}
		}
		else if( key.equals( GroupCall_Type ) ){
			groupCallType = sp.getInt(GroupCall_Type, GroupCall_Type_Muti);
		}
		else if( key.equals( Login_Type ) ){
			loginType = sp.getInt(Login_Type, Login_Type_IMEI);
		}
		
		Log.e(THIS_FILE, "onSharedPreferenceChanged" + toString());
	}

	public boolean isGsmFirst() {
		return isGsmFirst;
	}
	public boolean isPrivateSpeakerOn() {
		return privateSpeakerOn;
	}
//	public int getNegoTimer() {
//		return negoTimer;
//	}
//
//	public int getKeepAliveTimer_Wifi() {
//		return keepAliveTimer_Wifi;
//	}
//
//	public int getKeepAliveTimer_Mobile() {
//		return keepAliveTimer_Mobile;
//	}


	public boolean isVideoEnable( ) {
		return videoEnable;
	}
	protected void setVideoEnable( boolean videoEnable ) {
		this.videoEnable = videoEnable;
	}
	public int getTimerOption() {
		Log.e(THIS_FILE, "getTimerOption timerOption = " + timerOption);
		return timerOption;
	}
	
	public void setTimerOption(int timerOption) {
		Log.e(THIS_FILE, "setTimerOption timerOption = " + timerOption);
		this.timerOption = timerOption;
		
		SharedPreferences.Editor editor = sp.edit();
		editor.putString(Timer_Option, ""+timerOption);
		editor.commit();
	}
	public int getGroupCallType() {
		return groupCallType;
	}
	
	public void setGroupCallType(int groupCallType) {
		this.groupCallType = groupCallType;
		SharedPreferences.Editor editor = sp.edit();
		editor.putInt(GroupCall_Type, groupCallType);
		editor.commit();
		//CommDlg.showShortToast(ctx, "groupCallType = " + groupCallType);
	}
	
	public int getLoginType() {
		return loginType;
	}
	
	public void setLoginType(int loginType) {
		this.loginType = loginType;
		SharedPreferences.Editor editor = sp.edit();
		editor.putInt(Login_Type, loginType);
		editor.commit();

	}
	
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
//		strBuf.append("negoTimer = ").append(negoTimer).append("  ");
//		strBuf.append("keepAliveTimer_Wifi = ").append(keepAliveTimer_Wifi).append("  ");
//		strBuf.append("keepAliveTimer_Mobile = ").append(negoTimer).append("  ");
		strBuf.append("videoEnable = ").append(videoEnable).append("  ");
		strBuf.append("timer_Option = ").append(timerOption).append("  ");
		strBuf.append("groupCallType = ").append(groupCallType).append("  ");
		strBuf.append("loginType = ").append(loginType).append("  ");
		
		return strBuf.toString();
	}

}
